package run.siyuan.manag.controller.admin;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import run.siyuan.common.entity.Result;
import run.siyuan.common.exception.ExceptionProperties;
import run.siyuan.manag.entity.SysDept;
import run.siyuan.manag.entity.SysDict;
import run.siyuan.manag.entity.SysUser;
import run.siyuan.manag.service.SysDeptService;

import java.util.List;

/**
 * <p>
 * 机构表 前端控制器
 * </p>
 *
 * @author siyuan
 * @since 2021-01-13
 */
@Api(tags = "机构管理")
@RestController
@RequestMapping("/manag/sys-dept")
public class SysDeptController {

    @Autowired
    private SysDeptService deptService;
    @Autowired
    private ExceptionProperties properties;

    @ApiOperation("新增或修改")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody SysDept sysDept) {
        boolean result = false;
        if (StrUtil.isBlank(sysDept.getId())) {
            result = deptService.save(sysDept);
        } else {
            result = deptService.updateById(sysDept);
        }
        if (!result) {
            return Result.error(50000, properties.getCodeName("50000"));
        }
        return Result.success();
    }

    @ApiOperation(value = "详情", response = SysDept.class)
    @GetMapping("/getOne/{id}")
    public Result getDictById(@PathVariable Long id) {
        if (ObjectUtil.isEmpty(id)) {
            return Result.error(50001, properties.getCodeName("50001"));
        }
        return Result.success().data(deptService.getById(id));
    }

    @ApiOperation(value = "列表", response = SysDept.class)
    @GetMapping("/list")
    public Result list() {
        return Result.success().data(deptService.list());
    }

    @ApiOperation("批量删除")
    @DeleteMapping("/batchDelete")
    public Result batchDelete(@RequestBody List<String> idList) {
        if (!deptService.removeByIds(idList)) {
            return Result.error(50001, properties.getCodeName("50001"));
        }
        return Result.success();
    }

}

